package com.tudulist.dao.impl;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.NoResultException;
import javax.persistence.PersistenceContext;

import org.springframework.stereotype.Repository;

import com.tudulist.dao.UserDao;
import com.tudulist.model.User;

@SuppressWarnings("serial")
@Repository
public class UserDaoImpl implements UserDao {

	@PersistenceContext
	EntityManager em;

	@SuppressWarnings("unchecked")
	public List<User> findAll() {
		return em.createNamedQuery(User.USER_FIND_ALL).getResultList();
	}

	public User find(long id) {
		return (User) em.find(User.class, id);
	}

	public User find(String login) throws NoResultException {
		return (User) em.createNamedQuery(User.USER_FIND_BY_LOGIN).setParameter("login", login).getSingleResult();
	}

	public void save(User user) {
		em.merge(user);
	}

	public void remove(long id) {
		User user = (User) em.find(User.class, id);
		if (null != user) {
			em.remove(user);
		}
	}

}
